package com.mx.restful.beans;

import com.mx.restful.view.icon.Icons;
import com.intellij.psi.PsiMethod;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

/**
 * @Author: mengxiang.
 * @Date: 2020/6/22.
 * @Time: 11:37 上午.
 * @Description:
 */
public class Request {

    private final PsiMethod psiMethod;
    private HttpMethod method;
    private String path;
    private String comment;
    private Icon icon;

    public Request(HttpMethod method, @Nullable String path, @Nullable PsiMethod psiMethod) {
        this.setMethod(method);
        if (path != null) {
            this.setPath(path);
        }
        this.psiMethod = psiMethod;
    }

    public Request(HttpMethod method, @Nullable String path) {
        this.setMethod(method);
        if (path != null) {
            this.setPath(path);
        }
        this.psiMethod = null;
    }

    public Request(HttpMethod method, @Nullable String path, @Nullable PsiMethod psiMethod,String comment) {
        this.setMethod(method);
        this.comment = comment;
        if (path != null) {
            this.setPath(path);
        }
        this.psiMethod = psiMethod;
    }

    public PsiMethod getPsiMethod() {
        return psiMethod;
    }

    public void navigate(boolean requestFocus) {
        if (psiMethod != null) {
            psiMethod.navigate(requestFocus);
        }
    }

    public HttpMethod getMethod() {
        return method;
    }

    public void setMethod(HttpMethod method) {
        this.method = method;
        this.icon = Icons.getMethodIcon(method);
    }

    public Icon getIcon() {
        return icon;
    }

    public Icon getSelectIcon() {
        return Icons.getMethodIcon(this.method, true);
    }

    public String getPath() {
        return path;
    }

    public void setPath(@NotNull String path) {
        path = path.trim();
        if (!path.startsWith("/")) {
            path = "/" + path;
        }
        path = path.replaceAll("//", "/");
        this.path = path;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public void setParent(@NotNull Request parent) {
        if (this.method == null && parent.getMethod() != null) {
            this.setMethod(parent.getMethod());
        }
        String parentPath = parent.getPath();
        if(StringUtils.isNotBlank(parentPath) && "/".equals(parentPath)){
            parentPath = "";
        }
//        if (parentPath != null && parentPath.endsWith("/")) {
//            parentPath = "";
//        }
        this.setPath(parentPath + this.path);
    }

    @NotNull
    public Request copyWithParent(Request parent) {
        Request request = new Request(this.method, this.path, this.psiMethod,this.comment);
        if (parent != null) {
            request.setParent(parent);
        }
        return request;
    }

    @Override
    public String toString() {
        return "[" + method + "]" + path + "(" + icon + ")";
    }
}
